<?php
class CommentsController extends AppController {

	var $name = 'Comments';
	var $paginate = array('fields'=>array('Comment.id', 'Comment.post_id', 'Comment.user_id', 'Comment.title', 'Comment.body', 'Comment.published' ,'Comment.created' , 'Post.id' ,'Post.title', 'Post.url', 'User.id', 'User.username'),'limit'=>10, 'order'=>array('Post.created'=>'DESC'));

	function add() {
		if(!empty($this->data)) {
			if ($this->Conf->get('general.enable_captcha'))
			{
				if (!$this->Session->read('captcha') == $this->data['User']['captcha_check'] )
				{
					return false;
				}
			}
			$this->cleanUpFields();
			$this->Comment->create();
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('The Comment has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Comment could not be saved. Please, try again.');
			}
		} else {
			$this->Session->setFlash('Captcha codes don\'t match, please re-enter');
		}
	}
	$posts = $this->Comment->Post->generateList();
	$users = $this->Comment->User->generateList();
	$this->set(compact('posts', 'users'));
	}

	function edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Comment');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('The Comment saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Comment could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
		$posts = $this->Comment->Post->generateList();
		$users = $this->Comment->User->generateList();
		$this->set(compact('posts','users'));
	}

	function delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Comment');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Comment->del($id)) {
			$this->Session->setFlash('Comment #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}


	function admin_index() {
		$this->Comment->recursive = 0;
		$this->set('comments', $this->paginate());
	}

	function admin_view($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid Comment.');
			$this->redirect(array('action'=>'index'), null, true);
		}
		$this->set('comment', $this->Comment->read(null, $id));
	}

	function admin_add() {
		if(!empty($this->data)) {
			$this->cleanUpFields();
			$this->Comment->create();
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('The Comment has been saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Comment could not be saved. Please, try again.');
			}
		}
		$posts = $this->Comment->Post->generateList();
		$users = $this->Comment->User->generateList();
		$this->set(compact('posts', 'users'));
	}

	function admin_edit($id = null) {
		if(!$id && empty($this->data)) {
			$this->Session->setFlash('Invalid Comment');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if(!empty($this->data)) {
			$this->cleanUpFields();
			if($this->Comment->save($this->data)) {
				$this->Session->setFlash('The Comment saved');
				$this->redirect(array('action'=>'index'), null, true);
			} else {
				$this->Session->setFlash('The Comment could not be saved. Please, try again.');
			}
		}
		if(empty($this->data)) {
			$this->data = $this->Comment->read(null, $id);
		}
		$posts = $this->Comment->Post->generateList();
		$users = $this->Comment->User->generateList();
		$this->set(compact('posts','users'));
	}

	function admin_delete($id = null) {
		if(!$id) {
			$this->Session->setFlash('Invalid id for Comment');
			$this->redirect(array('action'=>'index'), null, true);
		}
		if($this->Comment->del($id)) {
			$this->Session->setFlash('Comment #'.$id.' deleted');
			$this->redirect(array('action'=>'index'), null, true);
		}
	}

}
?>